package com.IClass;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.IClass.IActivity.SplashActivity;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class NetUtil 
{

	/**
	 * @param uploadUrl 上传处理的页面
	 * @param srcPath 	图片文件路径
	 */
	public static  void UpLoadImage(String uploadUrl, String srcPath,NetUploadPictureListen listen) 
	{
		String end = "\r\n";
		String twoHyphens = "--";
		String boundary = "------WebKitFormBoundaryA9mFw94fYlrTjynQ";
		try 
		{
			File file = new File(srcPath);  

			URL url = new URL(uploadUrl);
			HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
			// 设置每次传输的流大小，可以有效防止手机因为内存不足崩溃
			// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
			//httpURLConnection.setChunkedStreamingMode(51200);// 128K
			// 允许输入输出流
			httpURLConnection.setDoInput(true);
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setUseCaches(false);
			// 使用POST方法
			httpURLConnection.setRequestMethod("POST");
			httpURLConnection.setRequestProperty("Connection", "keep-alive");
			httpURLConnection.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
			httpURLConnection.setRequestProperty("Accept:","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			httpURLConnection.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
			httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8");
			httpURLConnection.setRequestProperty("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3");
			httpURLConnection.setRequestProperty("Referer","http://ltll.gicp.net:8085/uploadfile.html");
			httpURLConnection.setRequestProperty("HOST", "ltll.gicp.net:8085");  
			httpURLConnection.setConnectTimeout(10 * 60 * 1000);
			String beginbound = "--" + boundary + end
				+ "Content-Disposition: form-data; name=\"file\"; filename=\"" + srcPath.substring(srcPath.lastIndexOf("/") + 1) + "\"" + end 
				+ "Content-Type: image/jpeg" + end
				+ end;
				
			
			String endbound =  end + "--" + boundary + end
				+ "Content-Disposition: form-data; name=\"submit\"" + end + end
				+ "Submit" + end 
				+ "--" + boundary + "--" + end;
			
			
			
			httpURLConnection.setRequestProperty("Content-Length", String.valueOf(file.length() + beginbound.length() + endbound.length()));


			DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
			out.writeBytes(beginbound);
			FileInputStream in = new FileInputStream(srcPath);
			byte[] buffer = new byte[1024]; // 8k
			int count = 0;
			int readcount = 0;
			int totalcount = in.available();
			// 读取文件
			while ((count = in.read(buffer)) != -1) {
				Log.i("UpLoadImage", "write count:" + readcount + ",total:" + totalcount );
				
				out.write(buffer, 0, count);
				out.flush();
				Log.i("UpLoadImage", "write count ok" );
				readcount+=count;
			}
			Log.i("UpLoadImage", "fis.close" );
			

			out.writeBytes(endbound);
			
			
			//dos.flush();
			Log.i("UpLoadImage", "flush ok" );
			InputStream is = httpURLConnection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			BufferedReader br = new BufferedReader(isr);
			String result = "";
			String t = br.readLine();
			while(t != null)
			{
				result += t;
				t = br.readLine();
			}
			
			
			
			Log.i("UpLoadImage", result );
			// Toast.makeText(this, result, Toast.LENGTH_LONG).show();
			listen.OnResult(result);
			is.close();
			in.close();
			out.close();

		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			Log.i("UpLoadImage", e.getMessage());
			// setTitle(e.getMessage());
		}
	}

}
